package com.xhs.repository;

import com.xhs.entity.Comment;
import com.xhs.entity.Content;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * 评论数据访问接口
 */
@Repository
public interface CommentRepository extends JpaRepository<Comment, Long> {
    
    /**
     * 根据内容查找评论（分页）
     */
    Page<Comment> findByContentAndParentIsNullOrderByCreatedAtDesc(Content content, Pageable pageable);
    
    /**
     * 根据父评论查找回复
     */
    @Query("SELECT c FROM Comment c WHERE c.parent.id = :parentId ORDER BY c.createdAt ASC")
    Page<Comment> findRepliesByParentId(@Param("parentId") Long parentId, Pageable pageable);
    
    /**
     * 统计内容的评论数
     */
    long countByContent(Content content);
    
    /**
     * 统计评论的回复数
     */
    long countByParent(Comment parent);
} 